use log::info;
use tokio::sync::{mpsc, oneshot};

async fn some_computation() -> String {
    "represents the result of the computation".to_string()
}

#[tokio::test]
async fn oneshot_test() {
    let (tx, rx) = oneshot::channel();

    tokio::spawn(async move {
        let res = some_computation().await;
        tx.send(res).unwrap();
    });

    // Do other work while the computation is happening in the background

    // Wait for the computation result
    let res = rx.await.unwrap();
    // let result = timeout(Duration::from_secs(3), rx.recv()).await;

    info!("result:  {}", res);
}


async fn some_computation2(input: u32) -> String {
    format!("the result of computation {}", input)
}

#[tokio::test]
async fn mpsc_test() {
    let (tx, mut rx) = mpsc::channel(100);

    tokio::spawn(async move {
        for i in 0..10 {
            let res = some_computation2(i).await;
            tx.send(res).await.unwrap();
        }
    });
    
    while let Some(res) = rx.recv().await {
        info!("got = {}", res);
    }
}